﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
[AddComponentMenu( "TheDice/MultiplySpriteColor" )]

public enum SPRITE_TYPE
{
    NONE = 0,
    NGUI,
    TK2D,
    
}


public class MultiplySpriteColor : MonoBehaviour 
{

    public bool                     Lock = false;
    public SPRITE_TYPE              SpriteType = SPRITE_TYPE.NGUI;
    public bool                     WithChildren = false;

    public Color                    MulEndColor = Color.gray;
    public float                    Speed = 1.0f;    
    Shader                          m_originalShader = null;
    float                           m_rAccuTime = 0.0f;
    
    List< tk2dSprite >              m_tk2dSpriteList = null;
    List< UIWidget >                m_nguiSpriteList = null;

    List< Color >                   m_startColorList = null;
    List< Color >                   m_curColorList = null;

    
	// Use this for initialization
	void Start () 
    {
        this.useGUILayout = false;
        if( this.Lock == false )
        {
            this.Apply( this.WithChildren );
        }           
	}

	// Update is called once per frame
	void Update () 
    {
        
        if( this.Lock == true )
        {
            return;
        }

        
        
        this.m_rAccuTime += Time.deltaTime * this.Speed;
        
        if( this.SpriteType == SPRITE_TYPE.NGUI )
        {
            for( int i = 0;     i < this.m_nguiSpriteList.Count;        ++i )
            {
                Color start_color = this.m_startColorList[ i ];
                Color cur_color = Color.Lerp( start_color, this.MulEndColor, this.m_rAccuTime );
                this.m_nguiSpriteList[ i ].color = cur_color;
            }
        }
        else if( this.SpriteType == SPRITE_TYPE.TK2D )
        {
            for( int i = 0;     i < this.m_tk2dSpriteList.Count;        ++i )
            {
                Color start_color = this.m_startColorList[i];
                Color cur_color = Color.Lerp(start_color, this.MulEndColor, this.m_rAccuTime);
                this.m_tk2dSpriteList[i].color = cur_color;
            }
        }       
        if( this.m_rAccuTime > 1.0f )
        {            
            this.m_rAccuTime = 0.0f;
            this.Lock = true;
        }      
	}
    

    public void Apply( bool bWithChildren )
    {
        if( this.enabled == false )
        {
            this.enabled = true;
        }

        this.WithChildren = bWithChildren;

        if (this.WithChildren == true)
        {
            this.CollectSprites();
        }
        else
        {
            this.CollectSingleSprite();
        }

        if( this.Speed <= 0.0001f )
        {
            if (this.SpriteType == SPRITE_TYPE.NGUI)
            {
                for (int i = 0; i < this.m_nguiSpriteList.Count; ++i)
                {                                        
                    this.m_nguiSpriteList[i].color = this.MulEndColor;
                }
            }
            else if (this.SpriteType == SPRITE_TYPE.TK2D)
            {
                for (int i = 0; i < this.m_tk2dSpriteList.Count; ++i)
                {                    
                    this.m_tk2dSpriteList[i].color = this.MulEndColor;
                }
            }       
            this.Lock = true;
        }
        else
        {
            this.m_rAccuTime = 0.0f;
            this.Lock = false;
        }
        
    }



    void CollectSprites()
    {
        if (this.SpriteType == SPRITE_TYPE.NGUI)
        {
            Component[] ngui_sprite_array = this.GetComponentsInChildren(typeof(UIWidget));
            if (ngui_sprite_array.Length == 0)
            {
                this.Lock = true;
                if (this.m_nguiSpriteList != null)
                {
                    this.m_nguiSpriteList.Clear();
                }
                return;
            }
            else
            {
                if (this.m_nguiSpriteList == null)
                {
                    this.m_nguiSpriteList = new List<UIWidget>(ngui_sprite_array.Length);
                    this.m_startColorList = new List<Color>(ngui_sprite_array.Length);
                }
                else
                {
                    this.m_nguiSpriteList.Clear();
                    this.m_startColorList.Clear();
                }
                for (int i = 0; i < ngui_sprite_array.Length; ++i)
                {
                    UIWidget cur_spr = ngui_sprite_array[i] as UIWidget;
                    Color start_col = cur_spr.color;
                    this.m_nguiSpriteList.Add(cur_spr);
                    this.m_startColorList.Add(start_col);
                }
            }
        }
        else if (this.SpriteType == SPRITE_TYPE.TK2D)
        {

            Component[] tk2d_sprite_array = this.GetComponentsInChildren(typeof(tk2dSprite));
            if (tk2d_sprite_array.Length == 0)
            {
                this.Lock = true;
                if (this.m_tk2dSpriteList != null)
                {
                    this.m_tk2dSpriteList.Clear();
                }
                return;
            }
            else
            {
                if (this.m_tk2dSpriteList == null)
                {
                    this.m_tk2dSpriteList = new List<tk2dSprite>(tk2d_sprite_array.Length);
                    this.m_startColorList = new List<Color>(tk2d_sprite_array.Length);
                }
                else
                {
                    this.m_tk2dSpriteList.Clear();
                    this.m_startColorList.Clear();
                }
                for (int i = 0; i < tk2d_sprite_array.Length; ++i)
                {
                    tk2dSprite cur_spr = tk2d_sprite_array[i] as tk2dSprite;
                    Color start_col = cur_spr.color;
                    this.m_tk2dSpriteList.Add(cur_spr);
                    this.m_startColorList.Add(start_col);
                }
            }

        }
        else
        {
            this.enabled = false;
            return;
        }
    }

    void CollectSingleSprite()
    {
        if (this.SpriteType == SPRITE_TYPE.TK2D)
        {
            if (this.m_tk2dSpriteList == null)
            {
                this.m_tk2dSpriteList = new List<tk2dSprite>(1);
                this.m_startColorList = new List<Color>(1);
            }
            else
            {
                this.m_tk2dSpriteList.Clear();
                this.m_startColorList.Clear();
            }

            tk2dSprite spr = this.GetComponent(typeof(tk2dSprite)) as tk2dSprite;
            if (spr != null)
            {
                this.m_tk2dSpriteList.Add(spr);
                this.m_startColorList.Add(spr.color);
            }

        }
        else if (this.SpriteType == SPRITE_TYPE.NGUI)
        {
            if (this.m_nguiSpriteList == null)
            {
                this.m_nguiSpriteList = new List<UIWidget>(1);
                this.m_startColorList = new List<Color>(1);
            }
            else
            {
                this.m_nguiSpriteList.Clear();
                this.m_startColorList.Clear();
            }

            UIWidget spr = this.GetComponent(typeof(UIWidget)) as UIWidget;
            if (spr != null)
            {
                this.m_nguiSpriteList.Add(spr);
                this.m_startColorList.Add(spr.color);
            }            
        }


    }

}
